package com.yitiao.controller;

import com.yitiao.service.BusinessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/business")
@Slf4j
@RequiredArgsConstructor
public class BusinessController {

    private final BusinessService businessService;

    /**
     * <h3>测试入口类</h3>
     * <p>正常：http://localhost:6102/business/purchase?rollback=false&count=2</p>
     *
     * <p>超库存：http://localhost:6102/business/purchase?rollback=false&count=12</p>
     *
     * <p>超余额：http://localhost:6102/business/purchase?rollback=false&count=8</p>
     *
     */
    @GetMapping(value = "/purchase")
    public String purchase(Boolean rollback, Integer count) {
        int orderCount = 10;
        if (count != null) {
            orderCount = count;
        }
        try {
            businessService.purchase("U100000", "C100000", orderCount,
                    rollback == null ? false : rollback.booleanValue());
        } catch (Exception exx) {
            return "Purchase Failed:" + exx.getMessage();
        }
        return "SUCCESS";
    }
}
